scanner {
# Extract memory usage information
  file("/proc/meminfo") {
    MemTotal = RegEx("^MemTotal:[\t ]*([0-9]+)[\t ]")
    MemAvailable = RegEx("^MemAvailable:[\t ]*([0-9]+)[\t ]")
    MemCache = RegEx( "^Cached:[\t ]*([0-9]+)[\t ]")
    MemBuff = Regex("^Buffers:[\t ]*([0-9]+)[\t ]")
  }

# Add up total and used swap capacity across all partitions
  file("/proc/swaps") {
    SwapTotal = RegEx("[\t ]([0-9]+)",Sum)
    SwapUsed = RegEx("[\t ][0-9]+[\t ]([0-9]+)",Sum)
  }

# Add up CPU utilization stats across all CPUs
  file("/proc/stat") {
    CpuUser = RegEx("^cpu [\t ]*([0-9]+)",Sum)
    CpuNice = RegEx("^cpu [\t ]*[0-9]+ ([0-9]+)",Sum)
    CpuSystem = RegEx("^cpu [\t ]*[0-9]+ [0-9]+ ([0-9]+)",Sum)
    CpuIdle = RegEx("^cpu [\t ]*[0-9]+ [0-9]+ [0-9]+ ([0-9]+)",Sum)
  }

  file("/sys/class/hwmon/hwmon7/temp1_input") {
    CPUTemp = Grab()
  }
  file("/sys/class/power_supply/BAT0/charge_full") {
    BatteryTotal = Grab(Sum)
  }
  file("/sys/class/power_supply/BAT0/charge_now") {
    BatteryLeft = Grab(Sum)
  }
  file("/sys/class/backlight/*/actual_brightness") {
    BacklightActual = Grab(Sum)
  }
  file("/sys/class/backlight/*/max_brightness") {
    BacklightMax = Grab(Sum)
  }
  file("/sys/class/bluetooth/*/rfkill*/state") {
    BluetoothState = Grab(Sum)
  }
  file("/proc/net/dev") {
    NetDev = RegEx("^[\t ]*(.*(?<!lo)):",First)
  }
  file("/proc/net/wireless") {
    WifiInterface = RegEx("^(.*):",First)
    WifiSignal = RegEx(":[\t ]*-?[0-9]+\.?[\t ]*-?[0-9]+\.?[\t ]*(-?[-0-9]+)",First)
  }
  file("/sys/class/net/*/carrier_*_count") {
    NetCount = Grab(Sum)
  }
  exec("iw dev;ip addr") {
    WifiSsid = RegEx("ssid (.*)$")
    NetIp = RegEx("inet ([0-9\./]*(?<!127.0.0.1/8)) ",First)
  }
}

define CpuUsage = (CpuUser-CpuUser.pval)/(CpuUser+CpuNice+CpuSystem+CpuIdle-
          CpuUser.pval-CpuNice.pval-CpuSystem.pval-CpuIdle.pval)*100
define Volume = Val(If(PaCtlSink.count=0 & Cached(PaVolume.count)>0,
      Cached(PaVolume),PaVolume))
define Mute = If(PaCtlSink.count=0 & Cached(PaMute.count)>0,
      Cached($PaMute),$PaMute)

define WifiQuality = (100 - (-20 - WifiSignal) * 77/65)


# Window Placer
placer {
  xorigin = 5  # place the first window at X% from the left
  yorigin = 5  # place the first window at X% from the top
  xstep = 5    # step by X% of desktop horizontally
  ystep = 5    # step by X% of desktop vertically
  children = true
}

# Task Switcher
switcher {
  interval = 700
  icons = true
  labels = false
  cols = 5
}

menu("winops") {
  item("focus", Focus );
  item("close", Close );
  item("minimize", [!Maximized] Minimize );
  item("unminimize", UnMinimize );
  item("maximize", Maximize );
  item("unmaximize", UnMaximize );
}

function("ToggleNet") {
  [UserState] UserState "off"
  [UserState] SetValue If($WifiInterface="",If($NetDev="","Disconnected",
    $NetDev + ": " + If(Cached(NetCount) = NetCount,Cached($NetIp), $NetIp)),
    $NetDev + ": " + If(Cached(NetCount) = NetCount,Cached($NetIp), $NetIp))
  [!UserState] UserState "on"
  [!UserState] SetValue If($WifiInterface="",If($NetDev="","Disconnected",
        $NetDev + ": " + 
        If(Cached(NetCount) = NetCount,Cached($NetIp), $NetIp) + " "),
        If(Cached(NetCount) = NetCount,Cached($WifiSsid), $WifiSsid) +
        " (" + Str(WifiQuality,0) + "%) ")
}

function("ToggleInhibitor") {
  [!IdleInhibit] IdleInhibit "on"
  [!IdleInhibit] SetStyle "idle_inhibitor_activated"
  [!IdleInhibit] SetValue ""
  [!IdleInhibit] SetTooltip "activated"
  [IdleInhibit] IdleInhibit "off"
  [IdleInhibit] SetStyle "idle_inhibitor"
  [IdleInhibit] SetValue ""
  [IdleInhibit] SetTooltip "deactivated"
}

function("RecordCmd") {
  Exec ".config/waybar/wf-rec.py --click" 
}

function("ToggleRecorder") {
  [UserState] UserState "off"
  [UserState] Function "RecordCmd"
  [UserState] SetTooltip "not recording"
  [UserState] SetValue ""
  [!UserState] UserState "on"
  [!UserState] SetTooltip "recording"
  [!UserState] Function "RecordCmd"
  [!UserState] SetValue "R"
}

#function("SfwbarInit") {
#  SetLayer "bottom"
#}

# Panel layout

layout {
  button {
		#value = "open-menu-symbolic"
		value = "icons/milis-logo.svg"
		style = "startmenu"
		action = exec "nwg-drawer" 
		tooltip = "Menu"
  }
  include("showdesktop.widget")
  pager {
    style = "pager"
    rows = 1
    preview = true
    numeric = true
  }
  taskbar {
    rows = 1
    css = "* { -GtkWidget-hexpand: true; }" # take up empty space, set icon size
    icons = true     # display icons
    labels = true    # display titles
    action[3] = Menu "winops"
  }
  
  label {
    style = "panel_item"
    value = ''
    action[1] = Exec ".config/swappy/area.sh"
  }
  
  label {
    style = "panel_item"
    interval = 6000
    action = Function "ToggleNet"
    action[0] = Function "ToggleNet"
    action[1] = Exec "connman-gtk"
  }

  label {
    style = "panel_item"
    interval = 500
    value = If(BluetoothState = 1," ","")
  }

  #label {
  #  style = "panel_item"
  #  interval = 10000
   # value = Str(CpuUsage,0) + '% '
  #}

  #label {
   # style = "panel_item"
   # interval = 30000
   # value = Str((MemTotal-MemAvailable)/MemTotal*100,0) + '% '
  #}

  label {
    style = "panel_item"
    interval = 10000
    value = Str(CPUTemp/1000,0) + '°C ' +
          if(CpuTemp/1000 < 20, "",
          if(CpuTemp/1000 < 40, "", ""))
  }

  label {
    style = "panel_item"
    interval = 500
    value = Str(BacklightActual/BacklightMax*100,0) +
          '% '
    action[4] = exec "light -A 5;light -O"
    action[5] = exec "light -U 5;light -O"
  }

  label {
    style = "panel_item"
    interval = 60000
    value = Str(BatteryLeft/BatteryTotal*100,0) + '% ' +
          if(BatteryLeft/BatteryTotal < .2,"", 
          if(BatteryLeft/BatteryTotal < .4,"", 
          if(BatteryLeft/BatteryTotal < .6,"", 
          if(BatteryLeft/BatteryTotal < .8,"",""))))
  }
  include("weather.widget")
  include("pulse.widget")
    
  label {
    style = "panel_item"
    value = Time("%d %b %a %H:%M")
    action[1] = exec "gsimplecal"
  }

  tray {
    rows = 1
  }
  
  label {
    css = "* { padding-bottom: 1mm; }"
    value = ""
    action[1] = exec "nwgbar -b 272822"
  }
  

}

#CSS
* {
  /* `otf-font-awesome` is required to be installed for icons */
  font-family: Roboto, Helvetica, Arial, sans-serif;
  font-size: 13px;
}

window#sfwbar {
  -GtkWidget-direction: bottom;
  background-color: rgba(43, 48, 59, 0.5);
  /* border-bottom: 3px solid rgba(100, 114, 125, 0.5); */
  color: #ffffff;
  transition-property: background-color;
  transition-duration: .5s;
}

button#taskbar_normal grid {
  -GtkWidget-hexpand: false;
  padding-right: 0px;
  margin-right: 0px;
}
button#taskbar_normal image, button#taskbar_active image, button#taskbar_normal:hover image {
  min-width: 0.4cm;
  min-height: 0.4cm;
}

button#taskbar_normal label, button#taskbar_active label, button#taskbar_normal:hover label {
  -GtkWidget-vexpand: true;
  -GtkWidget-hexpand: false;
  padding-left: 0.75mm;
  padding-top: 0px;
  padding-bottom: 0px;
  font: 0.4cm Sans;
}

button#taskbar_normal , button#taskbar_active , button#taskbar_normal:hover {
  padding-left: 0.75mm;
  padding-top: 0.5mm;
  padding-bottom: 0.5mm;
  background-image: none;
  /* border-radius: 3; */
  border: none;
  border-image: none;
  -GtkWidget-hexpand: false;
  
  background-image: none; background-color: transparent;
}

button#taskbar_active {
  background-color: #bbddff;
  color: #000000;
}

button#taskbar_normal:hover {
  background-color: #ffffff;
}

eventbox { background-image: none; background-color: transparent; }

button#pager_normal *, button#pager_visible *, button#pager_focused * {
  padding: 0px;
  color: #ffffff;
}

button#pager_normal, button#pager_visible, button#pager_focused {
  padding: 0 5px;
  background-color: transparent;
  background-image: none;
  color: #ffffff;
  /* Use box-shadow instead of border so the text isn't offset */
  box-shadow: inset 0 -3px transparent;
  /* Avoid rounded borders under each workspace name */
  border: none;
  border-radius: 0;
  min-height: 30px;
}

button#pager_focused {
  background-color: #64727D;
  box-shadow: inset 0 -3px #ffffff;
}

#mode {
  background-color: #64727D;
  border-bottom: 3px solid #ffffff;
}

#panel_item,
#pulseaudio,
#pulseaudio_muted,
#tray,
#mode {
  padding: 0 8px;
  color: #ffffff;
  margin-right: 1px;
}

#window,
#workspaces {
  margin: 0 4px;
}

#panel_item {
  background-image: none; background-color: transparent; border: none;
}

@keyframes blink {
  to {
      background-color: #ffffff;
      color: #000000;
  }
}

#battery.critical:not(.charging) {
  background-color: #f53c3c;
  color: #ffffff;
  animation-name: blink;
  animation-duration: 0.5s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

label:focus {
  background-color: #000000;
}

#network_disconnected {
  background-color: #f53c3c;
}

#pulseaudio {
  background-image: none; background-color: transparent;
  
}

#pulseaudio_muted {
  background-color: #90b1b1;
  color: #2a5c45;
}

image#tray_active, image#tray_passive, image#tray_attention {
  background-image: none; background-color: transparent;
  padding: 7px 5px;
}

image#tray_passive {
  -gtk-icon-effect: dim;
}

image#tray_attention {
  -gtk-icon-effect: highlight;
  background-image: none; background-color: transparent;
}


button#tray_active,
button#tray_passive,
button#tray_attention {
  background-image: none;background-color: transparent;
  border: 0px;
  padding: 0px;
  margin: 0px;
  border-image: none;
  border-radius: 0px;
  outline-style: none;
  box-shadow: none;
}

button#tray_active image,
button#tray_passive image,
button#tray_attention image {
  min-width: 5mm;
  min-height: 5mm;
  padding: 4px;
}

#language {
  background: #00b093;
  color: #740864;
  padding: 0 5px;
  margin: 0 5px;
  min-width: 16px;
}

#keyboard-state {
  background: #97e1ad;
  padding: 0 0px;
  margin: 0 5px;
  min-width: 16px;
}

#keyboard-state > label {
  padding: 0 5px;
}

#keyboard-state > label.locked {
  background: rgba(0, 0, 0, 0.2);
}

#hidden {
  -GtkWidget-visible: false;
}

button#pager_preview {
  background-image: none;
  border-radius: 0;
  border-image: none;
  border-color: #000000;
  border-width: 0.25mm;
  color: #777777;
  min-width: 5cm;
  min-height: 2.8125cm;
}

grid#switcher_active *, grid#switcher_active, grid#switcher_active * * {
  min-width: 1.25cm;
  min-height: 1.25cm;
  border-image: none;
  padding: 1.25mm;
  background-color: #2980b9;
  border-radius: 1.25mm;
}

grid#switcher_normal *, grid#switcher_normal, grid#switcher_normal * * {
  min-width: 1.25cm;
  min-height: 1.25cm;
  border-image: none;
  padding: 1.25mm;
}

window#switcher { 
  border-style: solid;
  border-width: 0.25mm;
  border-color: #000000;
  border-radius: 1.25mm; 
  padding: 1.25mm; 
  -GtkWidget-hexpand: true;
  background-color: rgba(43, 48, 59, 0.5);
  border: 3px solid rgba(100, 114, 125, 0.5);
  color: #ffffff;
  transition-property: background-color;
  transition-duration: .5s;
}

grid#switcher { 
  border-radius: 1.25mm;
  padding: 1.25mm;
}
